package br.com.news.mail;

import java.util.List;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import br.com.news.rss.RmiFactory;
import br.com.news.utils.database.beans.AnuncioBean;
import br.com.news.utils.database.beans.ContatoBean;
import br.com.news.utils.service.exception.ServiceException;

public class JobSendContato extends QuartzJobBean {

	@Autowired
    private EmailSenderContato emailSender;
	
	@Override
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {

		List<ContatoBean> contatos = RmiFactory.getContatoService()
				.buscaContatosNaoEnviados();

		for (ContatoBean contato : contatos) {
			emailSender.sendEmail("alexandreudicf@gmail.com",
					"alexandreudicf@gmail.com", contato.getNome()
							+ " acaba de enviar contato", contato);
			contato.setFlagEnvioEmail(true);

			try {
				RmiFactory.getContatoService().update(contato);
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	public void setEmailSender(EmailSenderContato emailSender) {
		this.emailSender = emailSender;
	}
	
	

}
